package com.ctshk.rpc.train.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.service.mapper.CtsBaseMapper;
import com.ctshk.rpc.train.dto.RecommendedTrainDTO;
import com.ctshk.rpc.train.dto.ReservationTrainPageDTO;
import com.ctshk.rpc.train.entity.TrainProduct;
import com.ctshk.rpc.train.req.RecommendedBusTicketReq;
import com.ctshk.rpc.train.req.ReservationTrainPageReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 火车产品表 Mapper 接口
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-11
 */
public interface TrainProductMapper extends CtsBaseMapper<TrainProduct> {

    /**
     * 查询火车票推荐列表(至多15个)
     * @return
     * @param req
     */
    @Select("<script>"+"select b.id trainProductId,b.railway_corporation_name,b.departure_city_name," +
            "b.arrive_city_name,b.boarding_station_name,b.alighting_station_name," +
            "b.supplier_id,b.product_source," +
            "d.price \n" +
            "from t_train_product_cost a \n" +
            "left join t_train_product b on a.train_product_id = b.id \n" +
            "left join t_train_product_cost_date c on a.id  = c.cost_id \n" +
            "left join t_train_product_cost_price d on a.id =d.cost_id \n" +
            "WHERE b.is_deleted = 0 \n" +
            "<if test=\"req.dataPermissionUserIds != null\">AND (" +
            "b.create_id in " +
            "<foreach collection='req.dataPermissionUserIds' item='id' open='(' separator=',' close=')'>"+
            "#{id}"+
            "</foreach>"+
            "or b.charge_person_id in " +
            "<foreach collection='req.dataPermissionUserIds' item='id' open='(' separator=',' close=')'>"+
            "#{id}"+
            "</foreach> )</if> \n"+
            "AND b.recommendation_status = 1 \n" +
            "AND c.date_begin &lt;=now() and c.date_end &gt;=now()\n" +
            "AND d.passenger_id = 1 \n" +
            "group by b.id order by b.gmt_create desc limit 15 \n" +
            "</script>")
    List<RecommendedTrainDTO> queryRecommendedTrainTicket(@Param("req") RecommendedBusTicketReq req);



    /**
     * 预订中心-查询火车票列表
     * @param req
     * @return
     */
    @Select("<script>"+"select b.id trainProductId,b.product_source,b.railway_corporation_name," +
            "b.departure_time,b.terminus_time,b.train_number_id,b.train_number_name,b.boarding_station_id,b.boarding_station_name," +
            "b.alighting_station_name,b.alighting_station_id,b.charge_person_id,b.create_id," +
//            "c.vehicle_type," +
//            "b.price_type," +
//            "b.price productPrice," +
//            "c.estimated_time," +
            "b.intended_for,b.supplier_name,b.intended_for_name,b.seat_level_name,d.id costId,d.stock \n" +
            "from t_train_product b \n" +
//            "left join t_train_line c on b.line_id =c.id \n" +
//            "left join t_train_number n on b.train_number_id =n.id \n" +
            "left join t_train_product_cost d on b.id = d.train_product_id \n" +
            "left join t_train_product_cost_date e on d.id = e.cost_id \n" +
            "left join t_train_product_strategy s on b.id = s.train_product_id \n" +
            "left join t_train_product_strategy_date sd on sd.increase_strategy_id = s.id \n" +
            "WHERE b.is_deleted = 0 \n" +
            "AND b.on_shelf_status = 1 \n" +
            "<if test=\"req.queryDate != null\">AND e.date_begin &lt;=#{req.queryDate} and e.date_end &gt;=#{req.queryDate}</if>\n" +
            "<if test=\"req.queryDate != null\">AND sd.date_begin &lt;=#{req.queryDate} and sd.date_end &gt;=#{req.queryDate}</if>\n" +
            //"<if test=\"req.queryDate != null\">AND b.departure_time &lt;=#{req.tomorrowQueryDate} and b.departure_time &gt;=#{req.queryDate}</if>\n" +
            "<if test=\"req.railwayCorporationName != null and req.railwayCorporationName != ''\">AND b.railway_corporation_name =#{req.railwayCorporationName}</if> \n" +
            "<if test=\"req.departureCityName != null and req.departureCityName != ''\">AND b.departure_city_name =#{req.departureCityName}</if> \n" +
            "<if test=\"req.arriveCityName != null and req.arriveCityName != ''\">AND b.arrive_city_name =#{req.arriveCityName}</if> \n" +
            "<if test=\"req.boardingStationName != null and req.boardingStationName != ''\">AND b.boarding_station_name =#{req.boardingStationName}</if> \n" +
            "<if test=\"req.alightingStationName != null and req.alightingStationName != ''\">AND b.alighting_station_name =#{req.alightingStationName}</if> \n" +
            "<if test=\"req.dataPermissionUserIds != null\">AND (" +
            "b.create_id in " +
            "<foreach collection='req.dataPermissionUserIds' item='id' open='(' separator=',' close=')'>"+
            "#{id}"+
            "</foreach>"+
            "or b.charge_person_id in " +
            "<foreach collection='req.dataPermissionUserIds' item='id' open='(' separator=',' close=')'>"+
            "#{id}"+
            "</foreach> )</if> \n"+
            "group by b.id order by b.departure_time asc \n" +
            "</script>")
    IPage<ReservationTrainPageDTO> queryReservationBusList(@Param("req") ReservationTrainPageReq req, IPage<ReservationTrainPageDTO> agentPage);
}
